package cs3343.project.family.UI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.CharacterController;
import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;
import cs3343.project.family.ProfileManager;

/**
 * The Class CharacterFrame.
 */
public class CharacterFrame extends JFrame implements ActionListener, ItemListener {
	
	/** The gc. */
	private GameController gc;
	
	/** The character controller. */
	private CharacterController characterController;

	/** The content pane. */
	private JPanel contentPane;	
	
	/** The btn_create profile. */
	private JButton btn_createProfile;	
	
	/** The cover. */
	private JLabel cover;	
	
	/** The picture. */
	private JLabel[] picture;
	
	/** The btn_start game. */
	private JButton btn_startGame;
	
	/** The combo list. */
	private JComboBox[] comboList;
	
	/** The combo. */
	private JComboBox<String> combo = null;
	
	/** The combo activate num. */
	private int comboActivateNum = 0; 

	/**
	 * Create the frame.
	 *
	 * @param gc the gc
	 */
	public CharacterFrame(GameController gc) {
		this.gc = gc;
		characterController = this.gc.getCharacterController();		
		initUI();
		prepareProfileList();
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,1));
		panel.setBorder(BorderFactory.createTitledBorder(ConfigManager.getLangString("legendControlPanel"))); 
		JPanel title = new JPanel();
		JPanel playerPane = new JPanel();
		title.setLayout(new BorderLayout());
		playerPane.setLayout(new GridLayout(2,3));
		
		btn_createProfile = new JButton();
		btn_createProfile.setText(ConfigManager.getLangString("btnCreateProfile"));
		btn_createProfile.addActionListener(this);
		
		cover = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "char.jpg"));

		//btn_startGame.setRolloverEnabled(true);
		//btn_startGame.setIcon(enter);
		//btn_startGame.addActionListener(this);
		
		btn_startGame = new JButton();
		btn_startGame.setText(ConfigManager.getLangString("btnStartGame"));
		btn_startGame.addActionListener(this);
		
		title.add(btn_createProfile, BorderLayout.WEST);
		title.add(cover, BorderLayout.CENTER);
		title.add(btn_startGame, BorderLayout.EAST);
		title.setBorder(BorderFactory.createRaisedBevelBorder());
		
		//Character Choose Box
		JPanel playerBox = null;
		picture = new JLabel[6];
		comboList = new JComboBox[ConfigManager.MAX_PLAYER];

		for (int i = 0; i < ConfigManager.MAX_PLAYER; i++) {
			playerBox = new JPanel();
			playerBox.setLayout(new BorderLayout());
			picture[i] = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "ran.jpg"));
			combo = new JComboBox(ProfileManager.profileArr);
			combo.addItemListener(this);
			
			if (i != 0)
				combo.setEnabled(false);
			
			comboList[i] = combo;
			playerBox.setBorder(BorderFactory.createTitledBorder(ConfigManager.getLangString("legendProfile") + (i + 1)));
			playerBox.add(picture[i],BorderLayout.CENTER);
			if (i < ConfigManager.MAX_PLAYER)
				playerBox.add(combo,BorderLayout.SOUTH);
			playerPane.add(playerBox);
		}
		
		panel.add(title);
		panel.add(playerPane);
		contentPane.add(panel);
		
		gc.getUiController().frameCentering(this);
	}
	
	/**
	 * Inits the ui.
	 */
	private void initUI() {
		setResizable(false);
		setTitle(ConfigManager.getLangString("characterFrameTitle"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300, 20, 800, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}
	
	/**
	 * Prepare profile list.
	 */
	private void prepareProfileList() {
		ProfileManager.readFromProfile();
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn_startGame) {		
			for (int i = 0; i < ConfigManager.MAX_PLAYER; i++) {
				if (comboList[i].getSelectedIndex() != 0) {
					characterController.addPlayer(ProfileManager.profiles.get(String.valueOf(comboList[i].getSelectedIndex())), i);
					characterController.setNumOfPlayer(i + 1);
				}
			}
				
			if (characterController.getNumOfPlayer() > 1) {
				boolean samePlayer = false;
				for (int i = 0; i < characterController.getNumOfPlayer(); i++) {
					for (int j = i + 1; j < characterController.getNumOfPlayer(); j++)
						if (characterController.samePlayer(characterController.getPlayer(i), characterController.getPlayer(j)))
							samePlayer = true;
				}
				if (!samePlayer) {
					gc.getMapController().setupMap(characterController.getNumOfPlayer());
					gc.getUiController().showGameFrame(characterController.getNumOfPlayer());
					this.dispose();
				}
				else
					JOptionPane.showMessageDialog(null, ConfigManager.getLangString("errMsgChooseplayer"), ConfigManager.getLangString("errorTitle"), 1);
			}
			else {
				JOptionPane.showMessageDialog(null, ConfigManager.getLangString("errMsgChooseplayer"), ConfigManager.getLangString("errorTitle"), 1);
			}
		}
		else if (e.getSource() == btn_createProfile) {
			gc.getUiController().showProfileCreateFrame();
		}
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	@Override
	public void itemStateChanged(ItemEvent e) {		
		if (comboList[comboActivateNum].getSelectedIndex() != 0)
			comboList[++comboActivateNum > 5 ? comboActivateNum = 5 : comboActivateNum].setEnabled(true);
		
		for (int i = 0; i < ConfigManager.MAX_PLAYER; i++) {
			for (int j = 0; j < ProfileManager.profiles.size() - 1; j++) {
				if (comboList[i].getSelectedIndex() == 0)
					picture[i].setIcon(new ImageIcon(ConfigManager.IMAGE_PATH + "ran.jpg"));
				else {
					String profileName = ProfileManager.profiles.get(String.valueOf(comboList[i].getSelectedIndex())).getName();
					if (profileName.equals(comboList[i].getSelectedItem()))
						picture[i].setIcon(ProfileManager.profiles.get(String.valueOf(comboList[i].getSelectedIndex())).getImage());
				}
			}
		}
	}
	
	/**
	 * Gets the btn_start game.
	 *
	 * @return the btn_start game
	 */
	public JButton getBtn_startGame() {
		return this.btn_startGame;
	}
	
	/**
	 * Gets the combo list.
	 *
	 * @return the combo list
	 */
	public JComboBox[] getComboList() {
		return this.comboList;
	}
}
